<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"  v-on:click="toPageOne">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="systypes" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;"
              @row-dblclick="handleClose">
      <el-table-column type="selection" width="100">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="sn" label="类型编号" width="150" >
      </el-table-column>
      <el-table-column prop="name" label="类型名称" width="200" >
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button type="warning" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!--抽屉-->
    <el-drawer
        title="字典明细列表"
        :visible.sync="table"
        direction="rtl"
        size="50%">

      <el-table :data="sysdetail">
        <el-table-column prop="name" label="明细名称" width="100" >
        </el-table-column>
        <el-table-column prop="typeName.name" label="类型名称" width="100" >
        </el-table-column>
      </el-table>
    </el-drawer>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>

      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"  style="float:right;"
                     :page-size="pageSize"
                     :current-page="currentPage"
                     :total="total">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="类型编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="类型姓名" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="warning" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>

export default {
  data() {
    return {
      table: false,//默认关闭抽屉
      sysdetail:[], //Sysdetail

      // 标题
      title:'',
      //关键字（模糊查询）
      filters: {
        keyword: ''
      },
      systypes: [],//类型数据 systype
      total: 0,//总条数
      currentPage: 1,
      pageSize: 5,
      listLoading: false,
      //声明一个变量装选中的对象的集合
      sels:[],
      editFormVisible: false,//编辑界面是否显示
      editLoading: false,

      editFormRules: {
        sn: [
          {required: true, message: '请输入类型编号', trigger: 'change'}
        ],
        name: [
          {required: true, message: '请输入类型名称', trigger: 'change'}
        ],
      },
      //编辑界面数据
      editForm: {
        id: null,
        sn:'',
        name:'',
      },
    }
  },
  methods: {

    //抽屉
    handleClose(row) {
      this.table= true,//开启抽屉
          this.$http.get("/sysD/findDetailByTypeId/"+row.id).then(result=>{
            this.sysdetail=result.data
          }).catch(result=>{
            this.$message({
              message: '网络不通！',
              type: 'error'
            });
          })
    },
    //模糊查询
    toPageOne(){
      this.currentPage=1;
      this.getSystypes();
    },

    //当前页也改变
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getSystypes();
    },

    //获取类型列表
    getSystypes() {
      // 忙等开启
      this.listLoading = true;
      let para = {
        currentPage:this.currentPage,
        pageSize:this.pageSize,
        keyword:this.filters.keyword
      }
      this.$http.post("/sysT/",para)
          .then(result=>{

            this.systypes=result.data.rows;
            this.total=result.data.totals;
            // 忙等关闭
            this.listLoading = false;
          })
          .catch(result=>{
            this.$message({
              message: '网络不通！',
              type: 'error'
            });
          })
    },

    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //发送请求，并且携带参数id
        // debugger//断点
        this.$http.delete("/sysT/" + row.id).
        then(result=>{
          //关闭忙等
          this.listLoading = false;
          if(result.data.success){
            this.$message({
              message: '删除成功！',
              type: 'success'
            });
          }
          this.currentPage=1;
          this.getSystypes();
        }).catch(result=>{
          this.$message({
            message: '操作失败！',
            type: 'error'
          });
        })

      });
    },

    //显示编辑界面
    handleEdit: function (index, row) {
      this.title="编辑"
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);
    },

    //显示新增界面
    handleAdd: function () {
      this.title="新增"
      this.editFormVisible = true;
      this.editForm = {
        id:null,
        sn:'',
        name:''
      };
    },

    //编辑(新增)
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;//按钮忙等开启
            this.editFormVisible = false;//关闭模态框
            //==============================================================
            let para = Object.assign({},this.editForm);//如果编辑/新增封装的para对象中，他的parent_id存在并且是一个数组，我们才取最后一个
            //==============================================================
            //发送请求
            this.$http.put("/sysT",para).then(result=>{
              this.editLoading = false;//按钮忙等关闭
              if (result.data.success){ //如果添加
                this.$message({
                  message: result.data.msg,
                  type: 'success'
                })
              }else {
                this.$message({
                  message: result.data.msg,
                  type: 'error'
                })
              }
              this.currentPage=1;
              this.getSystypes();
            }).catch(result=>{
              this.$message({
                message: '网络不通！',
                type: 'error'
              })

            })
          });
        }
      });
    },

    //将选中的对象都存放在sels数组集合中去
    selsChange: function (sels) {
      this.sels = sels;
    },

    //批量删除
    batchRemove: function () {
      //声明一个变量装获取到的所有对象id，这里涉及到stream流
      //所有对象转map，然会遍历，并获取其每个对象想的id，因为integer类型数以不需要调用toString()方法
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //发送请求，并且携带参数id
        // aioxs异步请求+请求方式(路径 + 参数)/(路径,对象)
        this.$http.patch("/sysT/" ,ids).
        then(result=>{
          //关闭忙等
          this.listLoading = false;
          if(result.data.success){
            this.$message({
              message: '删除成功！',
              type: 'success'
            });
          }
          this.currentPage=1;
          this.getSystypes();
        }).catch(result=>{
          this.$message({
            message: '操作失败！',
            type: 'error'
          });
        })

      })
    },

  },
  mounted() {
    this.getSystypes();
  }
}

</script>

<style scoped>

</style>